// component/KJdetles/KJdtels.js
let http = require('../../utils/http')
const utils = require('../../utils/util')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        items:"",
        kj:"",
        userImg:"", 
        list:"",
        shareCode:false,
        countDown:{
            time:"",
            timeArray:"",
            endTime:""
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
       
        console.log(options);
        let {HeadIcon}=wx.getStorageSync('userinfo')
        let endTime = utils.dateToTime(options.endtime)
        this.setData({
            kj:options,
            userImg:HeadIcon,
            items:options,
            "countDown.endTime":endTime
        })
        console.log(options)

        this.countDownOpen()
        this.getKJ()
    },
    
    //用户砍价详情
    getKJ(){
        let {Id}=wx.getStorageSync('userinfo')
        let obj={
            user_id:Id, 
            bargain_id:this.data.kj.id,
            spec_id:this.data.kj.spec_id  
        }
        http.request("GET",'/bargain/help_user',obj).then((res)=>{
            console.log(res);
            if (res.data.ErrCode==0) {
               this.setData({
                   list:res.data.ResData
               })
            }else{
                console.error(res.data.errMsg)
            }
        })
        
    },
    // 提交订单
    topay(){
        let that=this
        console.log("提交订单");
        console.log(that.data.kj)
        let goods={
            count:1,
            imgUrl:that.data.kj.img,
            spec:{
                Name:that.data.kj.SpecName,
                Price:that.data.kj.Price,
                price:that.data.kj.money,
                goodsType:"kanjia",
                GoodsId: that.data.kj.goods_id
            },
            title:that.data.kj.title
        }
        wx.navigateTo({
          url: `/pages/product/confirm/confirm?source=kanjia&&spec_id=${this.data.kj.spec_id}&id=${this.data.kj.bargain_id}&bargain_id=${this.data.kj.id}&goods=${encodeURIComponent(JSON.stringify([goods]))}`,
        })
    },
    //邀请好友砍价
    invite(){
        let that=this
        let {Id}=wx.getStorageSync('userinfo')
        let sharekanjiaID=this.data.items.id
        console.log(this.data.items.bargain_id,this.data.items.spec_id,sharekanjiaID,"分享数据");
       wx.navigateTo({
         url: `/pages/product/detail/detail?source=kanjia&id=${that.data.items.bargain_id}&kanjiaCode=0&sharekanjiaID=${sharekanjiaID}&KJ=${that.data.items.spec_id}&itemCode=0`,
       })
    },
    /* 倒计时模块 */
    countDownOpen() {
        this.countDownStop()

        console.log('开户定时任务', this.data.countDown)

        this.countDownContent()

        this.data.countDown.timer = setInterval(() => {
            // console.log('倒计时定时任务')
            this.countDownContent()
        }, 1000)
    },
    /* 倒计时模块 */
    countDownStop() {
        console.log('停止定时任务', this.data.countDown)
        if (this.data.countDown.timer) {
            clearInterval(this.data.countDown.timer)
            this.data.countDown.timer = null
        }
    },
    /* 倒计时模块 */
    countDownContent() {
        let endTime = this.data.countDown.endTime
        let countDownNumber = endTime - parseInt(new Date().getTime() / 1000)
        // console.log(countDownNumber);
        // console.log(utils)
        this.setData({
            ['countDown.time']: countDownNumber,
            ['countDown.timeArray']: utils.numberToCountDown(countDownNumber, 40)
        })
        // console.log(this.data.countDown.timeArray);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})